//
//  RETProductSpecificationCell.m
//  Retail
//
//  Created by ChenJie on 4/1/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "RETProductSpecificationCell.h"
#import "OABase.h"

typedef NS_ENUM(NSUInteger, ComponentId) {
    TitleLabel,
    ValueLabel,
    Separator,
};

@interface RETProductSpecificationCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *valueLabel;
@property (nonatomic, strong) UIView *separator;

@end

@implementation RETProductSpecificationCell

#pragma mark - Lifecycle methods

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.valueLabel];
        [self.contentView addSubview:self.separator];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //
    self.titleLabel.frame = [self.viewModel.oa_layouts[@(TitleLabel)] CGRectValue];
    self.valueLabel.frame = [self.viewModel.oa_layouts[@(ValueLabel)] CGRectValue];
    self.separator.frame = [self.viewModel.oa_layouts[@(Separator)] CGRectValue];
}

- (void)prepareForReuse {
    [super prepareForReuse];
    
    //
}

#pragma mark - Public methods

#define kTitleFont [UIFont systemFontOfSize:18]
#define kValueFont [UIFont systemFontOfSize:18]
#define kMarginV 20
#define kSeparatorHeight 1

+ (CGFloat)oa_heightWithWidth:(CGFloat)cellWidth viewModel:(RETProductSpecificationCellViewModel *)viewModel {
    NSMutableDictionary *layouts = [NSMutableDictionary dictionary];
    CGFloat topMark = 0;
    
    topMark += kMarginV;
    
    CGFloat titleLabelWidthMax = cellWidth / 2;
    CGSize titleSize = [viewModel.title oa_sizeWithWidth:titleLabelWidthMax font:kTitleFont];
    CGRect titleFrame = CGRectMake(0, topMark, titleSize.width, titleSize.height);
    layouts[@(TitleLabel)] = OAValueWithRect(titleFrame);
    
    CGFloat valueLabelWidthMax = cellWidth / 2;
    CGSize valueSize = [viewModel.value oa_sizeWithWidth:valueLabelWidthMax font:kValueFont];
    CGFloat valueLabelLeft = cellWidth - valueSize.width;
    CGRect valueFrame = CGRectMake(valueLabelLeft, topMark, valueSize.width, valueSize.height);
    layouts[@(ValueLabel)] = OAValueWithRect(valueFrame);
    
    topMark += MAX(titleSize.height, valueSize.height);
    topMark += kMarginV;
    CGRect separatorFrame = CGRectMake(0, topMark, cellWidth, kSeparatorHeight);
    layouts[@(Separator)] = OAValueWithRect(separatorFrame);
    
    viewModel.oa_layouts = layouts;
    
    return CGRectGetMaxY(separatorFrame);
}

#pragma mark - Private methods



#pragma mark - Event response methods



#pragma mark - Delegate methods



#pragma mark - Setter

- (void)setViewModel:(RETProductSpecificationCellViewModel *)viewModel {
    _viewModel = viewModel;
    
    self.titleLabel.text = viewModel.title;
    self.valueLabel.text = viewModel.value;
}

#pragma mark - Getter

OAGetLabel(titleLabel, ^(UILabel *label){
    label.font = kTitleFont;
    label.textColor = OAColor(333333);
    label.numberOfLines = 0;
})

OAGetLabel(valueLabel, ^(UILabel *label){
    label.font = kValueFont;
    label.textColor = OAColor(666666);
    label.textAlignment = NSTextAlignmentRight;
    label.numberOfLines = 0;
})

OAGetView(separator, ^(UIView *view){
    view.backgroundColor = OAColor(dddddd);
})

#pragma mark - Memory Management Methods

- (void)dealloc {
}

@end
